package BanyuanClub.Work.Day0224.TeDate;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

public class Student implements Comparable<Student>{
    private String name;
    private Date joinSchool;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", joinSchool=" + joinSchool +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getJoinSchool() {
        return joinSchool;
    }

    public void setJoinSchool(Date joinSchool) {
        this.joinSchool = joinSchool;
    }

    public Student(String name, Date joinSchool) {
        this.name = name;
        this.joinSchool = joinSchool;
    }

    public static void main(String[] args) {
        Student[] sarr = new Student[10];
        for (int i = 0; i < 10; i++) {
            String name = "";
            for (int j = 0; j < 3; j++) {
                char c = (char) (Math.random()*26 + 'a');
                name += c;
            }
            Calendar c = Calendar.getInstance();
            int year = (int)(Math.random()*(2021-2017)+2017);
            int month = (int) (Math.random()*11+1);
            int day = (int)(Math.random()*31+1);
            c.set(year,month,day);
            Date joinSchool = c.getTime();
            sarr[i] = new Student(name,joinSchool);
        }
        System.out.println(Arrays.toString(sarr));
        Arrays.sort(sarr);
        System.out.println(Arrays.toString(sarr));
    }

    /**
     * Compares this object with the specified object for order.  Returns a
     * negative integer, zero, or a positive integer as this object is less
     * than, equal to, or greater than the specified object.
     *
     * <p>The implementor must ensure
     * {@code sgn(x.compareTo(y)) == -sgn(y.compareTo(x))}
     * for all {@code x} and {@code y}.  (This
     * implies that {@code x.compareTo(y)} must throw an exception iff
     * {@code y.compareTo(x)} throws an exception.)
     *
     * <p>The implementor must also ensure that the relation is transitive:
     * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies
     * {@code x.compareTo(z) > 0}.
     *
     * <p>Finally, the implementor must ensure that {@code x.compareTo(y)==0}
     * implies that {@code sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for
     * all {@code z}.
     *
     * <p>It is strongly recommended, but <i>not</i> strictly required that
     * {@code (x.compareTo(y)==0) == (x.equals(y))}.  Generally speaking, any
     * class that implements the {@code Comparable} interface and violates
     * this condition should clearly indicate this fact.  The recommended
     * language is "Note: this class has a natural ordering that is
     * inconsistent with equals."
     *
     * <p>In the foregoing description, the notation
     * {@code sgn(}<i>expression</i>{@code )} designates the mathematical
     * <i>signum</i> function, which is defined to return one of {@code -1},
     * {@code 0}, or {@code 1} according to whether the value of
     * <i>expression</i> is negative, zero, or positive, respectively.
     *
     * @param o the object to be compared.
     * @return a negative integer, zero, or a positive integer as this object
     * is less than, equal to, or greater than the specified object.
     * @throws NullPointerException if the specified object is null
     * @throws ClassCastException   if the specified object's type prevents it
     *                              from being compared to this object.
     */
    @Override
    public int compareTo(Student o) {
        return this.joinSchool.compareTo(o.joinSchool);
    }
}
